package me.firstapp.spi.shop.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.shop.JsonPointRechargeCard;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.shop.PointRechargeCardService;

@Service
public class PointRechargeCardServiceImpl extends BaseServiceImpl implements PointRechargeCardService {

	@Override
	public SingleObject<Object> addPointRechargeCard(Double pointValue, Double price, Integer counts) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pointValue", pointValue);
			parameters.put("price", price);
			parameters.put("counts", counts);
			JSONObject resultObject = api(parameters, "/pointRechargeCard/addPointRechargeCard.do", HttpMethod.POST,
					"积分充值卡新增");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonPointRechargeCard> findPointRechargeCardPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		PageObject<JsonPointRechargeCard> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/pointRechargeCard/findPointRechargeCardPage.do", "分页查询积分充值卡",
					searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonPointRechargeCard> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonPointRechargeCard> cardList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonPointRechargeCard card = JSONObject.toJavaObject(_jsonObject,
										JsonPointRechargeCard.class);
								cardList.add(card);
							}
						}
					}
					jsonPage.setList(cardList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> startUseCard(String ids, Long startDate, Long endDate) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("ids", ids);
			parameters.put("startDate", startDate);
			parameters.put("endDate", endDate);
			JSONObject resultObject = api(parameters, "/pointRechargeCard/startUseCard.do", HttpMethod.POST, "积分充值卡启用");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> startSellCard(Long id) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			JSONObject resultObject = api(parameters, "/pointRechargeCard/startSellCard.do", HttpMethod.POST,
					"积分充值卡设为在售");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> stopCard(Long id) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			JSONObject resultObject = api(parameters, "/pointRechargeCard/stopUse.do", HttpMethod.POST,
					"积分充值卡设为停用");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> selled(Long id) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			JSONObject resultObject = api(parameters, "/pointRechargeCard/selled.do", HttpMethod.POST,
					"积分充值卡设为已售");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
